<template>
    <div class="box">
        <div class="imgUpLoad">
            <h1>上传实拍图</h1>
            <input id="getimg" type="file" @change="handleFileChange" />
            <div class="chooseimg" v-if="img1">
                <img :src="img1" alt="" />
                <div class="changeagain" @click="choose">重新选择图片</div>
            </div>
            <div v-else class="tochoose" @click="choose">
                <img src="../../../img/上传图片.png" alt="" />
            </div>
            <div v-if="img1" class="makeConstituency" @click="() => (constituency = true,dots.length=0,getsloe=false)">点击建立选区</div>


            <h1>上传参考图</h1>
            <input id="getimg2" type="file" @change="handleFileChange2" />
            <div class="chooseimg" v-if="img2">
                <img :src="img2" alt="" />
                <div class="changeagain" @click="choose2">重新选择图片</div>
            </div>
            <div v-else class="tochoose" @click="choose2">
                <img src="../../../img/上传图片.png" alt="" />
            </div>
            <div><img :src="constituencyImg" height="100px" v-if="hasConstituencyImg" /></div>
        </div>

        <div class="imgshow">
            <div class="imgshowbox">
                <video v-if="waitvideo" style="width: 100%; height: 100%" @ended="handleVideoEnded">
                    <source src="../../../img/等待生成.mp4" type="video/mp4" />
                </video>
                <img v-if="showimg" style="height: 100%;" id="choosingimg" :src="imgs[0]" alt=""
                    @click="() => imgBig = true" />
            </div>
            <div class="imgshowbottom">
                <div v-if="waitvideo">
                    <div v-for="item in imgcount" :key="item" style="width: 140px;">正在生成图片</div>
                </div>
                <div v-if="showimg">
                    <div style="cursor: pointer;" v-for="(item, index) in imgs" :key="item">
                        <img :src="item" alt="" height="80px" @click="chooseimg(item, index)" />
                    </div>
                </div>
            </div>
        </div>
        <div class="tools">
            <div class="tool1" @click="onFocus1">
                正向提示词
                <el-input v-model="text1" style="width: 240px" :rows="2" type="textarea" :autosize="{ minRows: 3 }"
                    placeholder="将生成图中想要生成的关键词填写在此处,例如:篮球鞋，足球鞋" />
            </div>

            <div class="tool2" @click="onFocus2">
                负向提示词
                <el-input v-model="text2" style="width: 240px" :rows="2" type="textarea" :autosize="{ minRows: 3 }"
                    placeholder="将生成图中不想要生成的关键词填写在此处,例如:花朵，人物" />
            </div>

            <div class="tool3">
                画面质量
                <el-radio-group v-model="radio">
                    <el-radio value="1" size="large"><span style="color: white;">普通</span></el-radio>
                    <el-radio value="2" size="large"><span style="color: white;">高清</span></el-radio>
                    <el-radio value="3" size="large"><span style="color: white;">超清</span></el-radio>
                </el-radio-group>
            </div>

            <div class="tool35">
                图片变化幅度
                <div>
                    <el-slider v-model="quality" :step="10" />
                </div>
                <div style="display: flex; flex-direction: row; justify-content: space-between;">
                    <i>更像爆款图</i>
                    <i>更像提示词</i>
                </div>
            </div>

            <div class="tool4">
                画面张数
                <div>
                    <el-radio-group v-model="imgcount">
                        <el-radio value="2" border>2张</el-radio>
                        <el-radio value="4" border>4张</el-radio>
                    </el-radio-group>
                </div>
            </div>

            <el-button type="info" @click="submit">生成新款</el-button>

            <div :class="['dialog', nowinput !== 0 ? '' : 'hidden']" @mousedown="() => dialogBox.open()">
                AI口袋百宝箱
            </div>

            <DialogBox ref="dialogBox" @gettext="gettext" />
        </div>


        <div class="imgbig" v-if="imgBig">
            <div @click="() => imgBig = false"><el-icon style="color: white;font-size: 40px">
                    <Close />
                </el-icon></div>
            <img :src="imgs[i]" alt="">
        </div>
    </div>

    <div v-if="constituency" class="userchoose" @click="handleClick" @contextmenu.prevent="handleRightClick">
        <el-button @click="() => { getsloe = false; constituency = false; hasConstituencyImg = true }" type="info"
            v-if="getsloe">
            确定
        </el-button>
        <img :class="['userimg', !getsloe ? '' : 'changeopacity']" :src="img1" />
        <img ref="sloeimg" v-if="getsloe" class="sloe" src="../../../img/xie/选区.png" width="500px" />
        <div v-for="(dot, index) in dots" :key="index" :class="['dot', dot.color]"
            :style="{ top: dot.y + 'px', left: dot.x + 'px' }"></div>
    </div>



</template>

<script setup>
import { Close } from '@element-plus/icons-vue'
import { ref, nextTick, onMounted } from 'vue';
import DialogBox from './DialogBox.vue';

const imgBig = ref(false)
//正在看的图片
const i = ref(0)

const sloeimg = ref(null);
const dots = ref([]);
const getsloe = ref(false);
const errorimg = ref(false);
const vogueimg = ref(false);
const dialogBox = ref(null);
const img1 = ref('');
const img2 = ref('');
const constituency = ref(false);
const constituencyImg = require("../../../img/xie/选区.png");
const hasConstituencyImg = ref(false);
const text1 = ref('');
const text2 = ref('');
const radio = ref('2');
const quality = ref(50);

const imgcount = ref('2')

const waitvideo = ref(false);
const showimg = ref(false);
const imgs = ref([]);
const nowinput = ref(0);
const dialogVisible = ref(false);

const choose = () => document.getElementById("getimg").click();
const choose2 = () => document.getElementById("getimg2").click();

const handleFileChange = (e) => {
    if (!e.target.files[0]) return;
    errorimg.value = e.target.files[0]?.name === '嫦娥奔月.jpg';
    vogueimg.value = e.target.files[0]?.name === '爆款鞋图片.png';

    img1.value = URL.createObjectURL(e.target.files[0]);
};

const handleFileChange2 = (e) => {
    if (!e.target.files[0]) return;
    img2.value = URL.createObjectURL(e.target.files[0]);
};

const submit = () => {
    i.value = 0
    imgs.value.length = 0;
    showimg.value = false;
    waitvideo.value = true;
    nextTick(() => {
        document.querySelector("video")?.play();
    });
};

const handleVideoEnded = () => {
    waitvideo.value = false;
    for (let i = 0; i < imgcount.value; i++) {
        let file = require(`../../../img/xie/设计图${i + 1}.jpg`);

        imgs.value.push(file);
    }
    showimg.value = true;
};

const chooseimg = (url, index) => {
    document.getElementById('choosingimg').setAttribute('src', url)
    i.value = index

}


const handleClick = (e) => {
    // Check if left mouse button is clicked
    if (e.button === 0) {
        const dot = {
            x: e.pageX,
            y: e.pageY,
            color: "green",
        };
        dots.value.push(dot);
        if (dots.value.length == 3) {
            getsloe.value = true
        }
    }
};

const handleRightClick = (e) => {
    // Prevent default context menu
    e.preventDefault();
    // Check if right mouse button is clicked
    if (e.button === 2) {
        const dot = {
            x: e.pageX,
            y: e.pageY,
            color: "red",
        };
        dots.value.push(dot);
        if (dots.value.length == 3) {
            getsloe.value = true
        }
    }
};


const onFocus1 = () => {
    nowinput.value = 1;
    dialogVisible.value = true;
};

const onFocus2 = () => {
    nowinput.value = 2;
    dialogVisible.value = true;
};

const gettext = (text) => {
    if (nowinput.value === 1) text1.value = text;
    if (nowinput.value === 2) text2.value = text;
};

onMounted(() => { 


})


</script>

<style scoped lang="scss">
input { 
    visibility: hidden;
    position: absolute;
    left: 100px;
    top: 210px;
}

.box {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: start;

    .common {
        background-color: #504656;
        display: flex;
        flex-direction: column;
        border-radius: 10px;

    }

    .imgUpLoad {
        @extend .common;
        flex-basis: 15%;
        margin: 0 10px;
        align-items: center;

        h1 {
            color: white
        }

        .chooseimg {
            width: 120px;
            height: 120px;
            position: relative;

            img {
                width: 100%;
                height: 100%;
            }

            .changeagain {
                bottom: 0;
                width: 100%;
                height: 30px;
                opacity: 0.6;
                background-color: black;
                position: absolute;
                color: white;
                cursor: pointer;
                padding: 5px 0;
            }
        }

        .makeConstituency {
            background-color: rgb(107, 0, 174);
            font-size: 20px;
            font-weight: 600;
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
            color: white;
            cursor: pointer;
        }

        .tochoose {
            position: relative;
            cursor: pointer;

            img {
                width: 120px;
                height: 120px;
            }


        }

    }

    .imgshow {
        @extend .common;
        flex-basis: 60%;
        margin-right: 10px;
        padding: 10px;

        .imgshowbox {
            box-sizing: border-box;
            width: 100%;
            height: 80%;
            padding: 10px;
            background-color: #000;
            display: flex;
            justify-content: center;
            align-items: center;

            border: {
                style: dashed;
                width: 3px;
                color: rgb(101, 87, 114);
                radius: 10px;
            }
        }

        .imgshowbottom {
            div {
                display: flex;
                justify-content: center;
                align-items: center;
                margin: 0 30px;

                div {
                    margin: 10px 30px;
                    width: 80px;
                    height: 80px;
                    background-color: rgb(80, 50, 116);
                    font-size: 23px;
                    color: white;

                    padding: auto img {
                        cursor: pointer;
                    }

                }

            }

        }



    }

    .tools {
        @extend .common;
        flex-basis: 25%;
        justify-content: flex-start;
        padding: 0 10px;
        color: white;
        font-weight: 800;


        div {
            background-color: #383838;
            width: 100%;
            margin-bottom: 10px;
            padding: 5px;
            display: flex;
            flex-direction: column;

        }

        .tool1 {
            border-radius: 10px 10px 0 0;
        }

        .tool2 {}

        .tool3 {
            div {
                display: block;
                margin: 0;
                padding: 0;

                .el-slider {
                    padding: 10px 0 0 0;
                }
            }
        }

        .tool35 {
            div {
                display: block;
                margin: 0;
                padding: 0;

                .el-slider {
                    padding: 10px 0 0 0;
                }
            }

            i {
                font-size: 15px
            }
        }

        .tool4 {

            div {
                display: flex;
                flex-direction: row;
                justify-content: space-around;
                margin: 0;

                .el-tag {
                    width: 60px;
                    height: 30px;
                }
            }
        }

        .dialog {
            justify-self: end;
            align-self: flex-end;
            margin: 10px 0;
            width: 70px;
            height: 50px;
            padding: 10px;
            border-radius: 25px 0 0 25px;
            background-color: rgb(166, 0, 255);
            cursor: pointer;
            transition: all 0.5s;
        }
    }
}

.hidden {
    visibility: hidden;
}

.userchoose {
    position: fixed;
    width: 100%;
    height: 100%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 999;
    display: flex;
    justify-content: center;
    align-items: center;

    .el-button {
        position: absolute;
        left: 750px;
        top: 85%;

    }

    .userimg {
        width: 500px;
    }

    .sloe {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
    }

}

.changeopacity {
    opacity: 0.5;
}

.dot {
    position: absolute;
    width: 10px;
    height: 10px;
    border-radius: 50%;
}

.red {
    background-color: red;
}

.green {
    background-color: green;
}

.imgbig {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba($color: #000000, $alpha: 0.7);
    display: flex;
    justify-content: center;
    align-items: center;


    div {
        position: fixed;
        top: 20px;
        /* 调整关闭按钮的位置 */
        right: 40px;
    }

    img {
        max-width: 90%;
        /* 确保图片响应式调整 */
        max-height: 90%;
        object-fit: contain;
        /* 保持图片比例 */
    }
}
</style>